﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace DrawCurveDemo
{
	/// <summary>
	/// FhrCurveChart.xaml 的交互逻辑
	/// </summary>
	public partial class FhrCurveChart : UserControl
	{
		private int clickIndex = 0;
		private double offset = -1;
		private Point pointBorder;
		private bool isScrollEnd = true;
		private DispatcherTimer scrollEndTimer;

		public FhrCurveChart()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 添加实时心率数据
		/// </summary>
		/// <param name="data"></param>
		public void AddFhrData(FhrModel[] data)
		{
			if (data == null || data.Length == 0) return;

			if (!curveDraw.LastIndex.HasValue || data[^1].Index > curveDraw.LastIndex)
			{
				curveDraw.LastIndex = data[^1].Index;
			}

			for (int i = 0; i < data.Length; i++)
			{
				if (!curveDraw.DicFhrData.ContainsKey(data[i].Index.Value))
				{
					curveDraw.DicFhrData[data[i].Index.Value] = data[i];
				}
			}

			txtFhr1.Text = data[^1].Fhr1 + "";
			txtFhr2.Text = data[^1].Fhr2 + "";
			txtFhr3.Text = data[^1].Fhr3 + "";

			//更新画布宽度
			canvas.Width = curveDraw.LastIndex.Value / 2 * curveDraw.Xscale;
			borderSlider.Width = curveDraw.RenderSize.Width * curveDraw.RenderSize.Width / canvas.Width;
			borderSlider.Visibility = canvas.Width > curveDraw.RenderSize.Width ? Visibility.Visible : Visibility.Collapsed;

			if (isScrollEnd)
			{
				scrollViewer.ScrollToRightEnd();
				scrollViewer.UpdateLayout();
			}
		}

		private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
		{
			curveDraw.UpdateOffset(scrollViewer.HorizontalOffset);

			Canvas.SetLeft(borderSlider, scrollViewer.HorizontalOffset /
				((canvas.ActualWidth - curveDraw.RenderSize.Width) / (curveDraw.RenderSize.Width - borderSlider.ActualWidth)));
		}

		private void Canvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			clickIndex = 2;
		}

		private void Border_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			clickIndex = 1;
			pointBorder = e.GetPosition(borderSlider);
		}

		private void Canvas_MouseMove(object sender, MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
			{
				//拖动滑块
				if (clickIndex == 1)
				{
					isScrollEnd = false;
					scrollEndTimer?.Stop();

					Point point = e.GetPosition(canvasPanel);
					if (point.X - pointBorder.X <= 0)
					{
						scrollViewer.ScrollToHorizontalOffset(0);
					}
					else if (point.X - pointBorder.X >= canvasPanel.ActualWidth - borderSlider.ActualWidth)
					{
						scrollViewer.ScrollToHorizontalOffset(canvas.ActualWidth - canvasPanel.ActualWidth);
					}
					else if (point.X - pointBorder.X > 0 && point.X - pointBorder.X < canvasPanel.ActualWidth - borderSlider.ActualWidth)
					{
						var left = point.X - pointBorder.X;
						scrollViewer.ScrollToHorizontalOffset((canvas.ActualWidth - canvasPanel.ActualWidth) / (canvasPanel.ActualWidth - borderSlider.ActualWidth) * left);
					}
				}
				//拖动画布
				if (clickIndex == 2)
				{
					isScrollEnd = false;
					scrollEndTimer?.Stop();

					if (offset >= 0 && offset <= canvasPanel.ActualWidth)
					{
						scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset - (e.GetPosition(this).X - offset));
					}
					offset = e.GetPosition(this).X;
				}
			}
			else
			{
				if (clickIndex != 0) DoDragCurveEnd();

				clickIndex = 0;
				offset = -1;
			}
		}

		private void Canvas_MouseLeave(object sender, MouseEventArgs e)
		{
			if (clickIndex != 0) DoDragCurveEnd();
		}

		private void Canvas_PreviewMouseUp(object sender, MouseButtonEventArgs e)
		{
			DoDragCurveEnd();
		}

		/// <summary>
		/// 拖动结束事件，五秒后恢复走纸
		/// </summary>
		private void DoDragCurveEnd()
		{
			if (scrollViewer.HorizontalOffset >= scrollViewer.ScrollableWidth - 10)
			{
				scrollEndTimer?.Stop();
				isScrollEnd = true;
			}
			else
			{
				scrollEndTimer?.Stop();
				scrollEndTimer = new()
				{
					Interval = TimeSpan.FromSeconds(5)
				};
				scrollEndTimer.Tick += (s, e) =>
				{
					isScrollEnd = true;
					scrollEndTimer?.Stop();
				};
				scrollEndTimer.Start();
			}
		}
	}
}
